package view;

import java.awt.Dimension;
import java.beans.PropertyVetoException;

import javax.swing.BorderFactory;

import control.Controlador;
import control.ControladorTurma;

import model.Turma;
import patterns.Bean;
import util.Constantes;
import util.Detalhar;
import util.components.buttons.JButtonBuscar;
import util.components.buttons.JButtonCancelar;
import util.components.buttons.JButtonOK;

public class JanelaTurma extends javax.swing.JInternalFrame implements Bean{

	public static final int ADICIONAR = 0;
	public static final int REMOVER = 1;
	public static final int EDITAR = 2;
	public static final int CONSULTAR = 3;

	private Turma bean;
	private Controlador control;
	private boolean remover = false, editar = false, consultar = false;
	private int tipoJanela;

	public JanelaTurma(String title){
		this(title, false, false, false);
	}

	public JanelaTurma(String title, boolean remover, boolean editar, boolean consultar){
		super(title);
		this.remover = remover;
		this.editar = editar;
		this.consultar = consultar;
		control = new ControladorTurma(this); 
		initComponents();
		if(remover){
			setTipoJanela(JanelaTurma.REMOVER);
			detalhar();
		}else if(editar){
			setTipoJanela(JanelaTurma.EDITAR);
			detalhar();
		}else if(consultar){
			setTipoJanela(JanelaTurma.CONSULTAR);
			detalhar();
		}else{
			setTipoJanela(JanelaTurma.ADICIONAR);
		}
		control.registraEventos();
	}

	public void initComponents(){

		java.awt.GridBagConstraints gridBagConstraints;

		panelDadosTurma = new javax.swing.JPanel();
		panelBusca = new javax.swing.JPanel();
		panelBotao = new javax.swing.JPanel();
		btnOk = new JButtonOK(control, "Ok");
		btnCancelar = new JButtonCancelar(this, "Cancelar");
		btnBuscar = new JButtonBuscar(control, "Buscar");
		lblCodTurma = new javax.swing.JLabel();
		lblAno = new javax.swing.JLabel();
		lblSemetre = new javax.swing.JLabel();
		txtCodTurma = new javax.swing.JTextField();
		txtAno = new javax.swing.JTextField();
		txtSemestre = new javax.swing.JTextField();
		panelDadosDisciplina = new javax.swing.JPanel();
		comboCodDisc = new javax.swing.JComboBox();

		setLayout(new java.awt.GridBagLayout());

		panelBusca = new javax.swing.JPanel();
		panelBusca.setLayout(new java.awt.GridBagLayout());

		panelDadosTurma.setBorder(BorderFactory.createTitledBorder("Turma"));
		panelDadosTurma.setLayout(new java.awt.GridBagLayout());

		lblCodTurma.setText("Codigo da Turma:");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 1, 2, 5);
		panelDadosTurma.add(lblCodTurma, gridBagConstraints);

		txtCodTurma.setPreferredSize(new Dimension(250, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
		panelDadosTurma.add(txtCodTurma, gridBagConstraints);

		lblAno.setText("Ano:");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 1, 2, 5);
		panelDadosTurma.add(lblAno, gridBagConstraints);

		txtAno.setPreferredSize(new Dimension(40, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
		panelDadosTurma.add(txtAno, gridBagConstraints);

		lblSemetre.setText("Semetre:");
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 1, 2, 5);
		panelDadosTurma.add(lblSemetre, gridBagConstraints);

		txtSemestre.setPreferredSize(new Dimension(30, 20));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 5, 2, 5);
		panelDadosTurma.add(txtSemestre, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
		gridBagConstraints.weightx = 100.0;
		gridBagConstraints.weighty = 100.0;
		getContentPane().add(panelDadosTurma, gridBagConstraints);

		panelDadosDisciplina.setBorder(BorderFactory.createTitledBorder("Disciplina"));

		comboCodDisc.setPreferredSize(new Dimension(350,20));
		panelDadosDisciplina.add(comboCodDisc);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
		gridBagConstraints.weightx = 100.0;
		getContentPane().add(panelDadosDisciplina, gridBagConstraints);

		btnOk.setText("Ok");
		btnCancelar.setText("Cancelar");

		panelBotao.add(btnOk);
		panelBotao.add(btnCancelar);

		if(remover || editar || consultar){
			btnBuscar.setPreferredSize(new Dimension(80, 20));
			panelBusca.add(btnBuscar);

			gridBagConstraints = new java.awt.GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 2;
			gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
			gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints.insets = new java.awt.Insets(2, 1, 1, 1);
			gridBagConstraints.weightx = 100.0;
			getContentPane().add(panelBusca, gridBagConstraints);
		}

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.insets = new java.awt.Insets(1, 1, 1, 1);
		gridBagConstraints.weightx = 100.0;
		getContentPane().add(panelBotao, gridBagConstraints);

		setTitle(title);
		try {
			setIcon(true);
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		setVisible(true);
		setClosable(true);
		setResizable(true);
		setSize(new Dimension(Constantes.TAMANHO_JANELA_FILHA));
		setLocation(Constantes.POSICAO_X, Constantes.POSICAO_Y);
	}

	private javax.swing.JPanel panelDadosTurma;
	private javax.swing.JPanel panelDadosDisciplina;
	private javax.swing.JPanel panelBusca;
	private javax.swing.JPanel panelBotao;
	private javax.swing.JButton btnOk;
	private javax.swing.JButton btnCancelar;
	private javax.swing.JButton btnBuscar;
	private javax.swing.JLabel lblCodTurma;
	private javax.swing.JLabel lblAno;
	private javax.swing.JLabel lblSemetre;
	private javax.swing.JTextField txtCodTurma;
	private javax.swing.JTextField txtAno;
	private javax.swing.JTextField txtSemestre;
	private javax.swing.JComboBox comboCodDisc;

	public void detalhar(){
		Detalhar.campoTexto(txtAno);
		Detalhar.campoTexto(txtSemestre);
		Detalhar.detalharBotao(btnOk);
	}

	public void desdetalhar(){
		Detalhar.desdetalharCampoTexto(txtAno);
		Detalhar.desdetalharCampoTexto(txtSemestre);
		if(!consultar)
			Detalhar.desdetalharBotao(btnOk);
	}

	public void consultar(){
		panelBotao.remove(btnCancelar);
	}

	public Object getBean() {
		if(this.bean == null)
			bean = new Turma();

		if(txtCodTurma.getText() != null)
			this.bean.setCodTurma(Long.parseLong(txtCodTurma.getText()));

		if(txtAno.getText() != null)
			this.bean.setAno(Integer.parseInt(txtAno.getText()));
		return bean;
	}

	public void setBean(Object bean) {
		this.bean = (Turma)bean;

	}

	public int getTipoJanela() {
		return tipoJanela;
	}

	public void setTipoJanela(int tipoJanela) {
		this.tipoJanela = tipoJanela;
	}

	public javax.swing.JButton getBtnOk() {
		return btnOk;
	}

	public javax.swing.JButton getBtnCancelar() {
		return btnCancelar;
	}

	public javax.swing.JButton getBtnBuscar() {
		return btnBuscar;
	}

	public javax.swing.JTextField getTxtCodTurma() {
		return txtCodTurma;
	}

	public javax.swing.JTextField getTxtAno() {
		return txtAno;
	}

	public javax.swing.JTextField getTxtSemestre() {
		return txtSemestre;
	}

	public javax.swing.JComboBox getComboCodDisc() {
		return comboCodDisc;
	}
}
